package com.frank.oj.manager;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frank.model.entity.contest.ContestAnnouncement;
import com.frank.oj.common.exception.StatusFailException;
import com.frank.oj.model.dto.AnnouncementDTO;
import com.frank.oj.model.vo.AnnouncementVO;
import com.frank.oj.service.entity.common.AnnouncementEntityService;
import com.frank.oj.service.entity.contest.ContestAnnouncementEntityService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author frank
 * @Date 2024/4/8
 */
@Component
public class AdminContestAnnouncementManager {
    @Resource
    private AnnouncementEntityService announcementEntityService;
    @Resource
    private ContestAnnouncementEntityService contestAnnouncementEntityService;
    public IPage<AnnouncementVO> getAnnouncementList(Integer limit, Integer currentPage, Long cid) {
        if (currentPage == null || currentPage < 1) currentPage = 1;
        if (limit == null || limit < 1) limit = 10;
        return announcementEntityService.getContestAnnouncement(cid,false,limit,currentPage);
    }

    public void deleteAnnouncement(Long aid) throws StatusFailException {
        boolean isOk = announcementEntityService.removeById(aid);
        if (!isOk) {
            throw new StatusFailException("删除失败");
        }
    }

    public void addAnnouncement(AnnouncementDTO announcementDto) throws StatusFailException {
        boolean saveAnnouncement = announcementEntityService.save(announcementDto.getAnnouncement());
        boolean isOk = contestAnnouncementEntityService.saveOrUpdate(new ContestAnnouncement()
                .setAid(announcementDto.getAnnouncement().getId())
                .setCid(announcementDto.getCid()));

        if (!saveAnnouncement || !isOk) {
            throw new StatusFailException("添加失败");
        }
    }

    public void updateAnnouncement(AnnouncementDTO announcementDto) throws StatusFailException {

        boolean isOk = announcementEntityService.saveOrUpdate(announcementDto.getAnnouncement());
        if (!isOk) {
            throw new StatusFailException("更新失败");
        }
    }
}
